/* eslint-disable */
const forMatNum = (num) => {
  return num < 10 ? '0' + num : num + ''
}
const initPicker = {
  // 日期
  date: {
    init(start, end, mode = 'date', step, value, flag, disabled) {
      const aToday = new Date()
      let tYear; let tMonth; let tDay; let tHours; let tMinutes; let tSeconds; let defaultVal = []
      let initstartDate = new Date(start.toString())
      let endDate = new Date(end.toString())
      if (start > end) {
        initstartDate = new Date(end.toString())
        endDate = new Date(start.toString())
      }
      const startYear = initstartDate.getFullYear()
      const startMonth = initstartDate.getMonth() + 1
      const endYear = endDate.getFullYear()
      const years = []; const months = []; const days = []; const hours = []; const minutes = []; const seconds = []; let areas = []; let returnArr = []
      const curMonth = flag ? value[1] * 1 : (value[1] + 1)
      const dYear = aToday.getFullYear()
      const dMonth = aToday.getMonth() + 1
      const dDate = aToday.getDate()
      const totalDays = new Date(startYear, curMonth, 0).getDate()
      const dvalObj = {}
      switch (mode) {
        case 'half':
        case 'date':
        case 'yearMonth':
          const curYear = value[0]
          const curMonth = value[1]
          if (disabled) {
            for (let s = startYear; s <= dYear; s++) {
              years.push(s + '')
            }
            if (curYear == dYear) {
              for (let m = 1; m <= dMonth; m++) {
                months.push(forMatNum(m))
              }
            } else {
              for (let m = 1; m <= 12; m++) {
                months.push(forMatNum(m))
              }
            }
            if (curMonth == dMonth) {
              for (let d = 1; d <= dDate; d++) {
                days.push(forMatNum(d))
              }
            } else {
              for (let d = 1; d <= totalDays; d++) {
                days.push(forMatNum(d))
              }
            }
          } else {
            for (let s = startYear; s <= endYear; s++) {
              years.push(s + '')
            }
            for (let m = 1; m <= 12; m++) {
              months.push(forMatNum(m))
            }
            for (let d = 1; d <= totalDays; d++) {
              days.push(forMatNum(d))
            }
          }
          break
        default:
          for (let s = startYear; s <= endYear; s++) {
            years.push(s + '')
          }
          for (let m = 1; m <= 12; m++) {
            months.push(forMatNum(m))
          }
          for (let d = 1; d <= totalDays; d++) {
            days.push(forMatNum(d))
          }
          break
      }
      for (let h = 0; h < 24; h++) {
        hours.push(forMatNum(h))
      }
      for (let m = 0; m < 60; m += step * 1) {
        minutes.push(forMatNum(m))
      }
      for (let s = 0; s < 60; s++) {
        seconds.push(forMatNum(s))
      }
      if (flag) {
        returnArr = [
          years.indexOf(value[0]),
          months.indexOf(value[1]),
          days.indexOf(value[2]),
          hours.indexOf(value[3]),
          minutes.indexOf(value[4]) == -1 ? 0 : minutes.indexOf(value[4]),
          seconds.indexOf(value[5])
        ]
      }
      switch (mode) {
        case 'range':
          if (flag) {
            defaultVal = [returnArr[0], returnArr[1], returnArr[2], 0, returnArr[0], returnArr[1], returnArr[2]]
            return { years, months, days, defaultVal }
          } else {
            return { years, months, days }
          }
          break
        case 'date':
          if (flag) {
            defaultVal = [returnArr[0], returnArr[1], returnArr[2]]
            return { years, months, days, defaultVal }
          } else {
            defaultVal = [
              years.indexOf(value[0]) == -1 ? 0 : years.indexOf(value[0]),
              months.indexOf(value[1]) == -1 ? 0 : months.indexOf(value[1]),
              days.indexOf(value[2]) == -1 ? 0 : days.indexOf(value[2])
            ]
            return { years, months, days, defaultVal }
          }
          break
        case 'half':
          areas = [{
            label: '上午',
            value: 0
          }, {
            label: '下午',
            value: 1
          }]
          if (flag) {
            defaultVal = [returnArr[0], returnArr[1], returnArr[2], returnArr[3]]
            return { years, months, days, areas, defaultVal }
          } else {
            let idx = 0
            areas.map((v, k) => {
              if (v.label == value[3]) {
                idx = v.value
              }
            })
            defaultVal = [
              years.indexOf(value[0]) == -1 ? 0 : years.indexOf(value[0]),
              months.indexOf(value[1]) == -1 ? 0 : months.indexOf(value[1]),
              days.indexOf(value[2]) == -1 ? 0 : days.indexOf(value[2]),
              idx
            ]
            return { years, months, days, areas, defaultVal }
          }
          break
        case 'yearMonth':
          if (flag) {
            defaultVal = [returnArr[0], returnArr[1]]
            return { years, months, defaultVal }
          } else {
            defaultVal = [
              years.indexOf(value[0]) == -1 ? 0 : years.indexOf(value[0]),
              months.indexOf(value[1]) == -1 ? 0 : months.indexOf(value[1])
            ]
            return { years, months, defaultVal }
          }
          break
        case 'dateTime':
          if (flag) {
            defaultVal = returnArr
          } else {
            defaultVal = [
              years.indexOf(value[0]) == -1 ? 0 : years.indexOf(value[0]),
              months.indexOf(value[1]) == -1 ? 0 : months.indexOf(value[1]),
              days.indexOf(value[2]) == -1 ? 0 : days.indexOf(value[2]),
              hours.indexOf(value[3]) == -1 ? 0 : hours.indexOf(value[3]),
              minutes.indexOf(value[4]) == -1 ? 0 : minutes.indexOf(value[4]),
              seconds.indexOf(value[5]) == -1 ? 0 : seconds.indexOf(value[5])
            ]
          }
          return { years, months, days, hours, minutes, seconds, defaultVal }
          break
        case 'time':
          if (flag) {
            defaultVal = [returnArr[3], returnArr[4], returnArr[5]]
          } else {
            defaultVal = [
              hours.indexOf(value[0]) == -1 ? 0 : hours.indexOf(value[0]),
              minutes.indexOf(value[1]) == -1 ? 0 : minutes.indexOf(value[1]),
              seconds.indexOf(value[2]) == -1 ? 0 : seconds.indexOf(value[2])
            ]
          }
          return { hours, minutes, seconds, defaultVal }
          break
      }
    },
    initMonths: (year, disabled) => {
      const aDate = new Date()
      const dYear = aDate.getFullYear()
      const dMonth = aDate.getMonth() + 1
      const dDate = aDate.getDate()
      const flag = dYear == year
      const months = []
      if (disabled) {
        if (flag) {
          for (let m = 1; m <= dMonth; m++) {
            months.push(forMatNum(m))
          }
        } else {
          for (let m = 1; m <= 12; m++) {
            months.push(forMatNum(m))
          }
        }
      } else {
        for (let m = 1; m <= 12; m++) {
          months.push(forMatNum(m))
        }
      }
      return months
    },
    initDays: (year, month, disabled) => {
      const aDate = new Date()
      const dYear = aDate.getFullYear()
      const dMonth = aDate.getMonth() + 1
      const dDate = aDate.getDate()
      const flag = !!((dYear == year && dMonth == month))
      const totalDays = new Date(year, month, 0).getDate()
      const dates = []
      if (flag && disabled) {
        for (let d = 1; d <= dDate; d++) {
          dates.push(forMatNum(d))
        }
      } else {
        for (let d = 1; d <= totalDays; d++) {
          dates.push(forMatNum(d))
        }
      }
      return dates
    }
  },
  // 短期日期上下午
  limitHour: {
    init(dayStep = 7, dVal) {
      const startDate = new Date()
      const date = []; let areas = []; let hours = []
      const hour = new Date().getHours()
      const weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
      const arrs = []
      let defaultVal = []
      let d = 0; let a = 0; let h = 0
      for (let i = 0; i < dayStep; i++) {
        let year, month, day, weekday
        year = startDate.getFullYear()
        month = forMatNum(startDate.getMonth() + 1)
        day = forMatNum(startDate.getDate())
        weekday = weeks[startDate.getDay()]
        let label = ''
        switch (i) {
          case 0:
            label = '今天'
            break
          case 1:
            label = '明天'
            break
          case 2:
            label = '后天'
            break
          default:
            label = month + '月' + day + '日' + ' ' + weekday
            break
        }
        date.push({
          label: label,
          value: year + '-' + month + '-' + day,
          today: i == 0
        })
        startDate.setDate(startDate.getDate() + 1)
      }
      if (hour > 12) {
        areas = [{
          label: '下午',
          value: 1
        }]
      } else {
        areas = [{
          label: '上午',
          value: 0
        }, {
          label: '下午',
          value: 1
        }]
      }
      for (let k = hour > 12 ? hour - 12 : hour; k <= 12; k++) {
        hours.push({
          label: forMatNum(k),
          value: forMatNum(hour > 12 ? k + 12 : k)
        })
      }
      date.map((v, k) => {
        if (v.label == dVal[0]) {
          d = k
        }
      })
      if (d != 0) {
        areas = this.initAreas(date[d])
        hours = this.initHours(date[d], areas[a])
      }
      areas.map((v, k) => {
        if (v.label == dVal[1]) {
          a = k
        }
      })
      hours.map((v, k) => {
        if (v.label == dVal[2]) {
          h = k
        }
      })
      defaultVal = [d, a, h]
      return { date, areas, hours, defaultVal }
    },
    initAreas(date) {
      let areas = []
      const hour = new Date().getHours()
      if (date.today) {
        if (hour > 12) {
          areas = [{
            label: '下午',
            value: 1
          }]
        } else {
          areas = [{
            label: '上午',
            value: 0
          }, {
            label: '下午',
            value: 1
          }]
        }
      } else {
        areas = [{
          label: '上午',
          value: 0
        }, {
          label: '下午',
          value: 1
        }]
      }
      return areas
    },
    initHours(dateCol, hourCol) {
      const hours = []
      const hour = new Date().getHours()
      if (dateCol.today) {
        if (hourCol.value == 1 && hour <= 12) {
          for (let k = 1; k <= 12; k++) {
            hours.push({
              label: forMatNum(k),
              value: forMatNum(hourCol.value == 1 ? k + 12 : k)
            })
          }
        } else {
          for (let k = hour > 12 ? hour - 12 : hour; k <= 12; k++) {
            hours.push({
              label: forMatNum(k),
              value: forMatNum(hourCol.value == 1 ? k + 12 : k)
            })
          }
        }
      } else {
        for (let k = 1; k <= 12; k++) {
          hours.push({
            label: forMatNum(k),
            value: forMatNum(hourCol.value == 1 ? k + 12 : k)
          })
        }
      }
      return hours
    }
  },
  // 短期日期时间初始化
  limit: {
    init(dayStep = 7, startHour = 8, endHour = 20, minuteStep = 1, afterStep = 30, dVal) {
      const startDate = new Date()
      const bsDate = new Date(new Date().getTime() + afterStep * 60 * 1000)
      const date = []; let hours = []; const minutes = []
      let hour = bsDate.getHours()
      const minute = Math.floor(bsDate.getMinutes() / minuteStep) * minuteStep
      const weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
      let d = 0; let h = 0; let m = 0
      let defaultVal = []
      for (let i = 0; i < dayStep; i++) {
        let year, month, day, weekday
        year = startDate.getFullYear()
        month = forMatNum(startDate.getMonth() + 1)
        day = forMatNum(startDate.getDate())
        weekday = weeks[startDate.getDay()]
        let label = ''
        switch (i) {
          case 0:
            label = '今天'
            break
          case 1:
            label = '明天'
            break
          case 2:
            label = '后天'
            break
          default:
            label = month + '月' + day + '日' + ' ' + weekday
            break
        }
        date.push({
          label: label,
          value: year + '-' + month + '-' + day,
          flag: i == 0
        })
        startDate.setDate(startDate.getDate() + 1)
      }
      if (hour < startHour) {
        hour = startHour
      }
      if (hour > endHour) {
        hour = endHour
      }
      for (let k = hour * 1; k <= endHour * 1; k++) {
        hours.push({
          label: forMatNum(k),
          value: forMatNum(k),
          flag: k == hour
        })
      }
      for (let j = minute; j < 60; j += minuteStep * 1) {
        minutes.push({
          label: forMatNum(j),
          value: forMatNum(j)
        })
      }
      date.map((v, k) => {
        if (v.label == dVal[0]) {
          d = k
        }
      })
      if (d != 0) {
        hours = this.initHours(startHour = 8, endHour = 20, minuteStep = 1, afterStep = 30, date[d].value)
      }
      hours.map((v, k) => {
        if (v.label == dVal[1]) {
          h = k
        }
      })
      minutes.map((v, k) => {
        if (v.label == dVal[2]) {
          m = k
        }
      })
      defaultVal = [d, h, m]
      return { date, hours, minutes, defaultVal }
    },
    initHours(startHour = 8, endHour = 20, minuteStep = 1, afterStep = 30, date) {
      const hours = []
      const arr = date.split('-')
      const aDate = new Date()
      const dYear = aDate.getFullYear()
      const dMonth = aDate.getMonth() + 1
      const dDate = aDate.getDate()
      const bsDate = new Date(new Date().getTime() + afterStep * 60 * 1000)
      let hour = bsDate.getHours()
      const flag = !!((dYear == arr[0] && dMonth == arr[1] && dDate == arr[2]))
      if (hour > endHour) {
        hour = endHour
      }
      if (flag) {
        for (let k = hour * 1; k <= endHour * 1; k++) {
          hours.push({
            label: forMatNum(k),
            value: forMatNum(k),
            flag: k == hour
          })
        }
      } else {
        for (let k = startHour * 1; k <= endHour * 1; k++) {
          hours.push({
            label: forMatNum(k),
            value: forMatNum(k),
            flag: false
          })
        }
      }
      return hours
    },
    initMinutes(startHour = 8, endHour = 20, minuteStep = 1, afterStep = 30, date, hour) {
      const minutes = []
      const bsDate = new Date(new Date().getTime() + afterStep * 60 * 1000)
      const arr = date.split('-')
      const aDate = new Date()
      const dYear = aDate.getFullYear()
      const dMonth = aDate.getMonth() + 1
      const dDate = aDate.getDate()
      const dHour = bsDate.getHours()
      const minute = Math.floor(bsDate.getMinutes() / minuteStep) * minuteStep
      const flag = !!((dYear == arr[0] && dMonth == arr[1] && dDate == arr[2]))
      if (flag) {
        if (hour == dHour) {
          for (let j = minute; j < 60; j += minuteStep * 1) {
            minutes.push({
              label: forMatNum(j),
              value: forMatNum(j)
            })
          }
        } else {
          for (let j = 0; j < 60; j += minuteStep * 1) {
            minutes.push({
              label: forMatNum(j),
              value: forMatNum(j)
            })
          }
        }
      } else {
        for (let j = 0; j < 60; j += minuteStep * 1) {
          minutes.push({
            label: forMatNum(j),
            value: forMatNum(j)
          })
        }
      }
      return minutes
    }
  },
  // 选择区间初始化
  range: {
    init(start, end, value, flag) {
      const aToday = new Date()
      let tYear; let tMonth; let tDay; let tHours; let tMinutes; let tSeconds; let defaultVal = []
      let initstartDate = new Date(start.toString())
      let endDate = new Date(end.toString())
      if (start > end) {
        initstartDate = new Date(end.toString())
        endDate = new Date(start.toString())
      }
      const startYear = initstartDate.getFullYear()
      const startMonth = initstartDate.getMonth() + 1
      const endYear = endDate.getFullYear()
      const fyears = []; const fmonths = []; const fdays = []; const tyears = []; const tmonths = []; const tdays = []; const returnArr = []
      const curMonth = flag ? value[1] * 1 : (value[1] + 1)
      const totalDays = new Date(startYear, curMonth, 0).getDate()
      for (let s = startYear; s <= endYear; s++) {
        fyears.push(s + '')
      }
      for (let m = 1; m <= 12; m++) {
        fmonths.push(forMatNum(m))
      }
      for (let d = 1; d <= totalDays; d++) {
        fdays.push(forMatNum(d))
      }
      for (let s = startYear; s <= endYear; s++) {
        tyears.push(s + '')
      }
      for (let m = 1; m <= 12; m++) {
        tmonths.push(forMatNum(m))
      }
      for (let d = 1; d <= totalDays; d++) {
        tdays.push(forMatNum(d))
      }
      defaultVal = [
        fyears.indexOf(value[0]) == -1 ? 0 : fyears.indexOf(value[0]),
        fmonths.indexOf(value[1]) == -1 ? 0 : fmonths.indexOf(value[1]),
        fdays.indexOf(value[2]) == -1 ? 0 : fdays.indexOf(value[2]),
        0,
        tyears.indexOf(value[4]) == -1 ? 0 : tyears.indexOf(value[4]),
        tmonths.indexOf(value[5]) == -1 ? 0 : tmonths.indexOf(value[5]),
        tdays.indexOf(value[6]) == -1 ? 0 : tdays.indexOf(value[6])
      ]
      return {
        fyears,
        fmonths,
        fdays,
        tyears,
        tmonths,
        tdays,
        defaultVal
      }
    },
    initDays(year, month) {
      const totalDays = new Date(year, month, 0).getDate()
      const dates = []
      for (let d = 1; d <= totalDays; d++) {
        dates.push(forMatNum(d))
      }
      return dates
    }
  }
}

export default initPicker
